<?php
declare (strict_types = 1);

namespace app\model;

use think\Model;
use think\model\concern\SoftDelete;

/**
 * @property bool $type 类型:1=交友,2=活动
 * @property float $latitude 纬度
 * @property float $longitude 经度
 * @property int $browse 浏览数量
 * @property int $collect 收藏次数
 * @property int $deletetime
 * @property int $end_time 报名结束时间
 * @property int $hot 热度
 * @property int $id
 * @property int $like 点赞次数
 * @property int $play_end_time 活动结束时间
 * @property int $play_start_time 活动开始时间
 * @property int $start_time 报名开始时间
 * @property int $tags_id 分类ID
 * @property int $users_id 用户
 * @property int $users_num 参与人数
 * @property string $adcode
 * @property string $address 地址
 * @property string $content 介绍
 * @property string $createtime
 * @property string $hk_area_ids
 * @property string $images 图片
 * @property string $title 名称
 * @property string $unique 第三方唯一标识
 * @property-read \app\model\FriendsTags $tags
 * @property-read \app\model\FriendsUsers[] $join_users
 * @property-read \app\model\Users $users
 * @property-read \app\model\UsersCollect $users_collect
 * @property-read \app\model\UsersLike $users_like
 * @method static \think\db\Query onlyTrashed()
 * @method static \think\db\Query withTrashed()
 * @mixin \think\Model
 */
class Friends extends Model
{
    use SoftDelete;
    protected $deleteTime = 'deletetime';
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

//    public static function onBeforeInsert(Model $model): mixed
//    {
//        if (empty($model->getAttr('images'))){
//            if ($model->getAttr('type')==1){
//                $images = Config::where(['name'=>'friends1'])->value('value');
//            }
//            if ($model->getAttr('type')==2){
//                $images = Config::where(['name'=>'friends2'])->value('value');
//            }
//            $model->setAttr('images',$images);
//        }
//        return true;
//    }

    function getImagesAttr($value)
    {
        if (empty($value)){
            return '';
        }
        $value = explode(',',$value);
        foreach ($value as &$v){
            if (!str_contains($v, "http")) {
                $v=config('app.static_host').$v;
            }
        }
        return implode(',',$value);
    }


    function tags()
	{
		return $this->belongsTo(FriendsTags::class,'tags_id');
	}
	
	function users()
	{
		return $this->belongsTo(Users::class);
	}

    function joinUsers()
    {
        return $this->hasMany(FriendsUsers::class,'friends_id');
    }

    public function usersCollect()
    {
        return $this->hasOne(UsersCollect::class,'pro_id');

    }

    public function usersLike()
    {
        return $this->hasOne(UsersLike::class,'pro_id');
    }

}
